<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
    <el-dialog
      title="版本更新"
      :visible.sync="isUpdatedVersion"
      width="20%"
      center
      custom-class="connectIphone"
    >
      <div class="connectOur">
        <div class="serve">系统检测到有新版本，是否立即更新？</div>
        <div class="tip">温馨提示：点击取消之后,可稍后手动刷新更新版本。</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmUpdatedVersion"
          >确 定</el-button
        >
        <el-button @click="isUpdatedVersion = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "app",
  provide() {
    return {
      reload: this.reload,
    };
  },
  data() {
    return {
      isRouterAlive: true,
      isUpdatedVersion: false,
      currentVersion: "",
    };
  },
  // created() {
  //   setInterval(() => {
  //     this.checkVersion();
  //   }, 120000); // 2分钟检查一次
  // },
  methods: {
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(() => {
        this.isRouterAlive = true;
      });
    },
    checkVersion() {
      axios
        .get(`/js/version.json`, {
          headers: { "Cache-Control": "no-cache" },
        }) // 反正就是要请求到json文件的内容, 并且禁止缓存
        .then((res) => {
          this.currentVersion = res.data.version;
          const clientVersion = localStorage.getItem("_version_");
          // 和最新版本不同，刷新页面
          if (this.currentVersion !== clientVersion) {
            this.isUpdatedVersion = true;
          }
        });
    },
    confirmUpdatedVersion() {
      localStorage.setItem("_version_", this.currentVersion);
      window.location.reload();
    },
  },
};
</script>
<style lang="scss" >
.connectOur {
  margin-bottom: 10px;
  .tip {
    margin: 30px auto;
    text-align: center;
    color: #999;
    height: 30px;
    line-height: 50px;
    border-top: 1px solid #999;
    margin-bottom: -15px;
  }
}
</style>
